/*
 * *
 * author:cgm
 * date:18-5-21 下午1:29
 * /
 */

package com.kaili.controller.pda.v1;

import com.kaili.common.util.CommonUtil;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.common.util.ValidList;
import com.kaili.db.service.IHsOptRewbService;
import com.kaili.except.ResponseStatus;
import com.kaili.pojo.dto.rewb.RewbAddDTO;
import com.kaili.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * package name:com.kaili.controller
 * author:hasee
 * date :2018/5/21
 * project:micro-service
 */
@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController("rewbControllerP")
@Api(tags = "[pda]回单服务", description = "[pda]回单服务API")
public class RewbController {
    @Autowired
    IHsOptRewbService rewbService;
    @Autowired
    private MessageSource messageSource;

    //回单寄送,PDA用
    @PostMapping("/p/rewbsSend/bat")
    @ApiOperation(value = "[pda]批量新增回单寄送")
    List<Map<String, Object>> doSendConfirmCS(@Validated @RequestBody ValidList<RewbAddDTO> rewbAddDTO) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < rewbAddDTO.size(); i++) {
            RewbAddDTO bean = rewbAddDTO.get(i);
            ResponseStatus responseStatus = rewbService.doUpdateSendConfirmCS(bean);
            HashMap<String, Object> map = new HashMap<>();
            map.put("message", messageSource.getMessage(responseStatus.getMsg(), null, CommonUtil.getLocale()));
            map.put("status", responseStatus.getCode());
            map.put("ewbNo", bean.getEwbNo());
            list.add(map);
        }
        return list;
    }

    //回单验收,PDA用
    @PutMapping("/p/rewbsCheck/bat")
    @ApiOperation(value = "[pda]批量新增回单验收")
    List<Map<String, Object>> doCheckConfirmCS(@Validated @RequestBody ValidList<RewbAddDTO> rewbAddDTO) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < rewbAddDTO.size(); i++) {
            RewbAddDTO bean = rewbAddDTO.get(i);
            ResponseStatus responseStatus = rewbService.doUpdateCheckConfirmCS(bean);
            HashMap<String, Object> map = new HashMap<>();
            map.put("message", messageSource.getMessage(responseStatus.getMsg(), null, CommonUtil.getLocale()));
            map.put("status", responseStatus.getCode());
            map.put("ewbNo", bean.getEwbNo());
            list.add(map);
        }
        return list;
    }

    //回单返厂,PDA用
    @PutMapping("/p/rewbsReturn/bat")
    @ApiOperation(value = "[pda]批量新增回单返厂")
    List<Map<String, Object>> doReturnConfirmCS(@Validated @RequestBody ValidList<RewbAddDTO> rewbAddDTO) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < rewbAddDTO.size(); i++) {
            RewbAddDTO bean = rewbAddDTO.get(i);
            ResponseStatus responseStatus = rewbService.doUpdateReturnConfirmCS(bean);
            HashMap<String, Object> map = new HashMap<>();
            map.put("message", messageSource.getMessage(responseStatus.getMsg(), null, CommonUtil.getLocale()));
            map.put("status", responseStatus.getCode());
            map.put("ewbNo", bean.getEwbNo());
            list.add(map);
        }
        return list;
    }

    //根据回单号查询需要验收的数据
    @ApiOperation(value = "获取带验收/带返厂的托运单号", notes = "获取带验收/带返厂的托运单号")
    @GetMapping("/p/rewbsCheck")
    public List<Map> rewbsCheckEwbNo(@RequestParam("rewbNo") String rewbNo,
                                     @RequestParam("rewbStatus") Integer rewbStatus) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("siteId", LoginDetailUtils.getSiteId());
        map.put("rewbNo", rewbNo);
        map.put("rewbStatus", rewbStatus);
        List<Map> maps = rewbService.rewbsCheckEwbNo(map);
        System.out.println(maps);
        return rewbService.rewbsCheckEwbNo(map);
    }

}
